//
//  MCChaseNumberViewController.m
//  TLYL
//
//  Created by miaocai on 2017/6/16.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCChaseNumberViewController.h"
#import "MCZhuihaoTableViewCell.h"
#import "MCPickBottomButton.h"
#import "MCZhuihaoModel.h"
#import "MCPaySelectedLotteryHeaderView.h"
#import "MCMathUnits.h"

@interface MCChaseNumberViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

#pragma mark - property

/**segmentCotrol*/
@property(nonatomic,weak) UISegmentedControl *segmentCotrol;

/**下部滑动根view*/
@property(nonatomic,weak) UIScrollView *baseScrollView;

/**segmentCotrol下面的输入框和按钮的父控件*/
@property(nonatomic,weak) UIView *topFilterView;

/**数据源*/
@property(nonatomic,strong) NSMutableArray *selectedCheckBoxArray;

/**baseScrollView上的左边的tableview*/
@property (nonatomic,weak) UITableView *leftTableView;

/**baseScrollView上的中间的tableview*/
@property (nonatomic,weak) UITableView *middleTableView;

/**baseScrollView上的右边的tableview*/
@property (nonatomic,weak) UITableView *rightTableView;

/**当前cell的frame*/
@property (nonatomic,assign) CGRect currentRect;

/**底部view*/
@property (nonatomic,weak) UIView *bottomInfoView;

/**底部view中的著述*/
@property (nonatomic,weak) UILabel *middleZHUSHULabel;

/**底部view余额*/
@property (nonatomic,weak) UILabel *yuELabel;

/**被选中的cell*/
@property (nonatomic,strong) NSMutableArray *selectedArray;

/**倒计时*/
@property (nonatomic,weak) UIView *timerView;
/**同倍起始输入框*/
@property (nonatomic,weak) UITextField *startTF;
/**同倍追号输入框*/
@property (nonatomic,weak) UITextField *zhuiTF;

/**翻倍起始输入框*/
@property (nonatomic,weak) UITextField *startFanBeiTF;
/**翻倍追号输入框*/
@property (nonatomic,weak) UITextField *zhuiFanBeiTF;
/**翻倍隔输入框*/
@property (nonatomic,weak) UITextField *geFanBeiTF;
/**翻倍倍输入框*/
@property (nonatomic,weak) UITextField *beiFanBeiTF;

/**利润率起始输入框*/
@property (nonatomic,weak) UITextField *liRunStartTF;
/**利润率追号输入框*/
@property (nonatomic,weak) UITextField *liRunZhuiTF;
/**利润率输入框*/
@property (nonatomic,weak) UITextField *liRunTF;

@end

@implementation MCChaseNumberViewController

#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpUI];
    self.navigationItem.title = @"XXXXXX";
    self.view.backgroundColor = [UIColor whiteColor];
    NSLog(@"se----%@",self.dataSourceModel);

}

-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - setUpUI
- (void)setUpUI{
    
    [self setUpTimer];
    [self setUpSegmentControl];
    [self setUpBaseScorllView];
    [self setUpBottomView];
    [self setUpTopFilterView];
    [self setUpMiddleTabView];
    [self registerNotifacation];
    
    [self setDefaultValue];
}
/**
 *
 *顶部倒计时
 *
 **/
- (void)setUpTimer{
    MCPaySelectedLotteryHeaderView *timerView = [[MCPaySelectedLotteryHeaderView alloc] init];
    [self.view addSubview:timerView];
    timerView.frame=CGRectMake(0, MC_REALVALUE(64), G_SCREENWIDTH, [MCPaySelectedLotteryHeaderView computeHeight:nil]);
    timerView.dataSource=nil;
    self.timerView = timerView;
   
}

/**
 *
 *条件选择
 *
 **/
- (void)setUpSegmentControl{
    NSArray *arr = nil;
    if (self.dataSourceModel.dataSource.count > 1) {
       arr = @[@"同倍追号",@"翻倍追号"];
    } else {
       arr =  @[@"同倍追号",@"翻倍追号",@"利润率追号"];
    }
    //条件选择
    UISegmentedControl *segmentContrl = [[UISegmentedControl alloc] initWithItems:arr];
    [segmentContrl addTarget:self action:@selector(segmentControlSelected:) forControlEvents:UIControlEventValueChanged];
    self.segmentCotrol = segmentContrl;
    [self.view addSubview:segmentContrl];
    self.segmentCotrol.layer.cornerRadius = 10;
    self.segmentCotrol.layer.masksToBounds = YES;
    self.segmentCotrol.layer.borderColor = MC_SKYBLUE_COLOR.CGColor;
    self.segmentCotrol.layer.borderWidth = 1.0;
    self.segmentCotrol.tintColor = MC_SKYBLUE_COLOR;
    self.segmentCotrol.selectedSegmentIndex = 0;
    CGFloat padding = 54;
    
    [segmentContrl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(padding);
        make.right.equalTo(self.view.mas_right).offset(-padding);
        make.top.equalTo(self.timerView.mas_bottom).offset(MC_REALVALUE(15));
        make.height.equalTo(@(MC_REALVALUE(30)));
    }];
}

/**
 *
 *BaseScorllView
 *
 **/
- (void)setUpBaseScorllView{
    UIScrollView *baseScrollView = [[UIScrollView alloc] init];
    [self.view addSubview:baseScrollView];
    [baseScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.segmentCotrol.mas_bottom).offset(MC_REALVALUE(10));
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-MC_REALVALUE(49));
        
    }];
    baseScrollView.scrollEnabled = NO;
    if (self.dataSourceModel.dataSource.count > 1) {
        baseScrollView.contentSize = CGSizeMake(G_SCREENWIDTH * 2, baseScrollView.heiht);
    } else {
        baseScrollView.contentSize = CGSizeMake(G_SCREENWIDTH * 3, baseScrollView.heiht);
    }
    baseScrollView.pagingEnabled = YES;
    baseScrollView.bounces = NO;
    baseScrollView.showsVerticalScrollIndicator = NO;
    baseScrollView.showsHorizontalScrollIndicator = NO;
    self.baseScrollView = baseScrollView;
    self.baseScrollView.delegate = self;
}

/**
 *
 *TopFilterView
 *
 **/
- (void)setUpTopFilterView{
    
    [self setUpLeftFilterView];
    [self setUpTopMiddleFilterView];
    [self setUpRightFilterView];
   
    }

- (void)setUpLeftFilterView{
    //left
    UIView *topFilterView = [[UIView alloc] init];
    [self.baseScrollView addSubview:topFilterView];
    self.topFilterView = topFilterView;
    [topFilterView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.baseScrollView);
        make.height.equalTo(@(100));
        make.width.equalTo(@(G_SCREENWIDTH));
    }];
    
    
    UILabel *startLabel = [[UILabel alloc] init];
    [topFilterView addSubview:startLabel];
    startLabel.text = @"起始倍数:";
    startLabel.textColor = RGB(119, 119, 119);
    startLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [startLabel sizeToFit];
    CGFloat padding = MC_REALVALUE(20);
    [startLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topFilterView.mas_left).offset(padding);
        make.top.equalTo(topFilterView.mas_top).offset(MC_REALVALUE(5));
        
    }];
    
    UITextField *startTF = [[UITextField alloc] init];
    startTF.delegate = self;
    self.startTF = startTF;
    [topFilterView addSubview:startTF];
    startTF.layer.borderColor = RGB(119, 119, 119).CGColor;
    startTF.layer.borderWidth = 1.0f;
    [startTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(startLabel.mas_right);
        make.centerY.equalTo(startLabel);
        make.height.equalTo(@(MC_REALVALUE(23)));
        make.width.equalTo(@(MC_REALVALUE(68)));
    }];
    startTF.keyboardType = UIKeyboardTypeNumberPad;
    UITextField *zhuiTF = [[UITextField alloc] init];
    zhuiTF.delegate = self;
    [topFilterView addSubview:zhuiTF];
    zhuiTF.layer.borderColor = RGB(119, 119, 119).CGColor;
    zhuiTF.layer.borderWidth = 1.0f;
    [zhuiTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(startLabel);
        make.height.equalTo(@(MC_REALVALUE(23)));
        make.right.equalTo(topFilterView).offset(-padding);
        make.width.equalTo(@(MC_REALVALUE(68)));
        
    }];
    
    UILabel *zhuiLabel = [[UILabel alloc] init];
    [topFilterView addSubview:zhuiLabel];
    zhuiLabel.text = @"追号期数:";
    zhuiLabel.textColor = RGB(119, 119, 119);
    zhuiLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [zhuiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(zhuiTF.mas_left);
        make.top.equalTo(self.segmentCotrol.mas_bottom).offset(MC_REALVALUE(15));
        
    }];
    
    UILabel *infoLabel = [[UILabel alloc] init];
    self.zhuiTF = zhuiTF;
    [topFilterView addSubview:infoLabel];
    infoLabel.text = @"(最多可追120期)";
    zhuiTF.keyboardType = UIKeyboardTypeNumberPad;
    infoLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(12)];
    infoLabel.textColor = MC_SKYBLUE_COLOR;
    [infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(startTF.mas_bottom).offset(MC_REALVALUE(15));
        make.centerX.equalTo(topFilterView);
    }];
    UIButton *planBtn = [[UIButton alloc] init];
    [topFilterView addSubview:planBtn];
    [planBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(topFilterView);
        make.top.equalTo(infoLabel.mas_bottom).offset(MC_REALVALUE(6));
        make.width.equalTo(@(MC_REALVALUE(90)));
    }];
    
    [planBtn setTitle:@"生成追号计划" forState:UIControlStateNormal];
    planBtn.layer.cornerRadius = 4.0f;
    planBtn.layer.masksToBounds = YES;
    [planBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    planBtn.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    planBtn.backgroundColor = MC_SKYBLUE_COLOR;
    [planBtn addTarget:self action:@selector(tongBeiPlanBtnClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setUpTopMiddleFilterView{
    //Middle
    UIView *topFilterView = [[UIView alloc] init];
    [self.baseScrollView addSubview:topFilterView];
    [topFilterView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseScrollView);
        make.height.equalTo(@(MC_REALVALUE(100)));
        make.width.equalTo(@(G_SCREENWIDTH));
        make.left.equalTo(@(G_SCREENWIDTH));
    }];
    
    
    UILabel *startLabel = [[UILabel alloc] init];
    [topFilterView addSubview:startLabel];
    startLabel.text = @"起始倍数:";
    startLabel.textColor = RGB(119, 119, 119);
    startLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [startLabel sizeToFit];
    CGFloat padding = MC_REALVALUE(20);
    [startLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topFilterView.mas_left).offset(padding);
        make.top.equalTo(topFilterView.mas_top).offset(MC_REALVALUE(5));
        
    }];
    
    UITextField *startTF = [[UITextField alloc] init];
    [topFilterView addSubview:startTF];
    self.startFanBeiTF = startTF;
    startTF.layer.borderColor = RGB(119, 119, 119).CGColor;
    startTF.layer.borderWidth = 1.0f;
    startTF.keyboardType = UIKeyboardTypeNumberPad;
    [startTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(startLabel.mas_right);
        make.centerY.equalTo(startLabel);
        make.height.equalTo(@(MC_REALVALUE(23)));
        make.width.equalTo(@(MC_REALVALUE(68)));
    }];
    UILabel *beiLabel = [[UILabel alloc] init];
    [topFilterView addSubview:beiLabel];
    beiLabel.text = @"倍";
    beiLabel.textColor = RGB(119, 119, 119);
    beiLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [beiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(topFilterView).offset(-MC_REALVALUE(10));
        make.top.equalTo(self.segmentCotrol.mas_bottom).offset(MC_REALVALUE(15));
        
    }];
    
    UITextField *beiTF = [[UITextField alloc] init];
    self.beiFanBeiTF = beiTF;
    [topFilterView addSubview:beiTF];
    beiTF.keyboardType = UIKeyboardTypeNumberPad;
    beiTF.layer.borderColor = RGB(119, 119, 119).CGColor;
    beiTF.layer.borderWidth = 1.0f;
    [beiTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(startLabel);
        make.height.equalTo(@(MC_REALVALUE(23)));
        make.right.equalTo(beiLabel.mas_left).offset(-6);
        make.width.equalTo(@(MC_REALVALUE(40)));
        
    }];
        UILabel *XLabel = [[UILabel alloc] init];
        [topFilterView addSubview:XLabel];
        XLabel.text = @"X";
        XLabel.textColor = RGB(119, 119, 119);
        XLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
        [XLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(beiTF.mas_left).offset(-MC_REALVALUE(6));
            make.top.equalTo(self.segmentCotrol.mas_bottom).offset(MC_REALVALUE(15));
            
        }];

    UITextField *geTF = [[UITextField alloc] init];
    [topFilterView addSubview:geTF];
    self.geFanBeiTF = geTF;
    geTF.layer.borderColor = RGB(119, 119, 119).CGColor;
    geTF.layer.borderWidth = 1.0f;
    [geTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(startLabel);
        make.height.equalTo(@(MC_REALVALUE(23)));
        make.right.equalTo(XLabel.mas_left).offset(-MC_REALVALUE(6));
        make.width.equalTo(@(MC_REALVALUE(40)));
        
    }];
    geTF.keyboardType = UIKeyboardTypeNumberPad;
    UILabel *geLabel = [[UILabel alloc] init];
    [topFilterView addSubview:geLabel];
    geLabel.text = @"隔";
    geLabel.textColor = RGB(119, 119, 119);
    geLabel.font = [UIFont systemFontOfSize:14];
    [geLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(geTF.mas_left).offset(-MC_REALVALUE(6));
        make.top.equalTo(self.segmentCotrol.mas_bottom).offset(MC_REALVALUE(15));
        
    }];


    
    UILabel *zhuiLabel = [[UILabel alloc] init];
    [topFilterView addSubview:zhuiLabel];
    zhuiLabel.text = @"追号期数:";
    zhuiLabel.textColor = RGB(119, 119, 119);
    zhuiLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [zhuiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(startLabel.mas_left);
        make.top.equalTo(startLabel.mas_bottom).offset(MC_REALVALUE(30));
        
    }];
    
    UITextField *zhuiTF = [[UITextField alloc] init];
    zhuiTF.keyboardType = UIKeyboardTypeNumberPad;
    self.zhuiFanBeiTF = zhuiTF;
    [topFilterView addSubview:zhuiTF];
    zhuiTF.layer.borderColor = RGB(119, 119, 119).CGColor;
    zhuiTF.layer.borderWidth = 1.0f;
    [zhuiTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(zhuiLabel);
        make.height.equalTo(@(MC_REALVALUE(23)));
        make.left.equalTo(zhuiLabel.mas_right);
        make.width.equalTo(@(MC_REALVALUE(68)));
        
    }];
    
    UILabel *infoLabel = [[UILabel alloc] init];
    [topFilterView addSubview:infoLabel];
    infoLabel.text = @"(最多可追120期)";
    infoLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(12)];
    infoLabel.textColor = MC_SKYBLUE_COLOR;
    [infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(geLabel.mas_bottom).offset(MC_REALVALUE(15));
        make.centerX.equalTo(XLabel);
    }];
    UIButton *planBtn = [[UIButton alloc] init];
    [topFilterView addSubview:planBtn];
    [planBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(infoLabel);
        make.top.equalTo(infoLabel.mas_bottom).offset(MC_REALVALUE(6));
        make.width.equalTo(@(MC_REALVALUE(90)));
        make.right.equalTo(topFilterView).offset(-MC_REALVALUE(40));
    }];
    
    [planBtn setTitle:@"生成追号计划" forState:UIControlStateNormal];
    planBtn.layer.cornerRadius = 4.0f;
    planBtn.layer.masksToBounds = YES;
    [planBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    planBtn.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    planBtn.backgroundColor = MC_SKYBLUE_COLOR;
    [planBtn addTarget:self action:@selector(fanBeiPlanBtnClick) forControlEvents:UIControlEventTouchUpInside];

    
}

- (void)setUpRightFilterView{
    //Middle
    UIView *topFilterView = [[UIView alloc] init];
    [self.baseScrollView addSubview:topFilterView];
    [topFilterView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseScrollView);
        make.height.equalTo(@(MC_REALVALUE(100)));
        make.width.equalTo(@(G_SCREENWIDTH));
        make.left.equalTo(@(G_SCREENWIDTH *2));
    }];
    
    
    UILabel *startLabel = [[UILabel alloc] init];
    [topFilterView addSubview:startLabel];
    startLabel.text = @"起始倍数:";
    startLabel.textColor = RGB(119, 119, 119);
    startLabel.font = [UIFont systemFontOfSize:14];
    [startLabel sizeToFit];
    CGFloat padding = MC_REALVALUE(20);
    [startLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topFilterView.mas_left).offset(padding);
        make.top.equalTo(topFilterView.mas_top).offset(MC_REALVALUE(5));
        
    }];
    
    UITextField *startTF = [[UITextField alloc] init];
    [topFilterView addSubview:startTF];
    startTF.layer.borderColor = RGB(119, 119, 119).CGColor;
    startTF.layer.borderWidth = 1.0f;
    [startTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(startLabel.mas_right);
        make.centerY.equalTo(startLabel);
        make.height.equalTo(@(MC_REALVALUE(23)));
        make.width.equalTo(@(MC_REALVALUE(68)));
    }];
    self.liRunStartTF = startTF;
    startTF.keyboardType = UIKeyboardTypeNumberPad;
    UILabel *persentLabel = [[UILabel alloc] init];
    [topFilterView addSubview:persentLabel];
    persentLabel.text = @"%";
    persentLabel.textColor = RGB(119, 119, 119);
    persentLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [persentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(topFilterView).offset(-padding);
        make.top.equalTo(startLabel);
        
    }];
    
    UITextField *zhuiDITF = [[UITextField alloc] init];
    [topFilterView addSubview:zhuiDITF];
    self.liRunTF = zhuiDITF;
    zhuiDITF.keyboardType = UIKeyboardTypeNumberPad;
    zhuiDITF.layer.borderColor = RGB(119, 119, 119).CGColor;
    zhuiDITF.layer.borderWidth = 1.0f;
    [zhuiDITF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(startLabel);
        make.height.equalTo(@(MC_REALVALUE(23)));
        make.right.equalTo(persentLabel.mas_left);
        make.width.equalTo(@(MC_REALVALUE(60)));
        
    }];
    UILabel *zhuiDiLabel = [[UILabel alloc] init];
    [topFilterView addSubview:zhuiDiLabel];
    zhuiDiLabel.text = @"最低收益率:";
    zhuiDiLabel.textColor = RGB(119, 119, 119);
    zhuiDiLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [zhuiDiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(zhuiDITF.mas_left);
        make.top.equalTo(zhuiDITF);
        
    }];
    

    
    UILabel *zhuiLabel = [[UILabel alloc] init];
    [topFilterView addSubview:zhuiLabel];
    zhuiLabel.text = @"追号期数:";
    zhuiLabel.textColor = RGB(119, 119, 119);
    zhuiLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    [zhuiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(startLabel.mas_left);
        make.top.equalTo(startLabel.mas_bottom).offset(MC_REALVALUE(30));
        
    }];
    
    UITextField *zhuiTF = [[UITextField alloc] init];
    [topFilterView addSubview:zhuiTF];
    zhuiTF.layer.borderColor = RGB(119, 119, 119).CGColor;
    zhuiTF.layer.borderWidth = 1.0f;
    [zhuiTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(zhuiLabel);
        make.height.equalTo(@(MC_REALVALUE(23)));
        make.left.equalTo(zhuiLabel.mas_right);
        make.width.equalTo(@(MC_REALVALUE(68)));
        
    }];
    self.liRunZhuiTF = zhuiTF;
    zhuiTF.keyboardType = UIKeyboardTypeNumberPad;
    UILabel *infoLabel = [[UILabel alloc] init];
    [topFilterView addSubview:infoLabel];
    infoLabel.text = @"(最多可追120期)";
    infoLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(12)];
    infoLabel.textColor = MC_SKYBLUE_COLOR;
    [infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(zhuiDITF.mas_bottom).offset(MC_REALVALUE(15));
        make.right.equalTo(topFilterView).offset(-MC_REALVALUE(30));
    }];
    UIButton *planBtn = [[UIButton alloc] init];
    [topFilterView addSubview:planBtn];
    [planBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(infoLabel);
        make.top.equalTo(infoLabel.mas_bottom).offset(MC_REALVALUE(6));
        make.width.equalTo(@(MC_REALVALUE(90)));
        make.right.equalTo(topFilterView).offset(-MC_REALVALUE(40));
    }];
    
    [planBtn setTitle:@"生成追号计划" forState:UIControlStateNormal];
    planBtn.layer.cornerRadius = 4.0f;
    planBtn.layer.masksToBounds = YES;
    [planBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    planBtn.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    planBtn.backgroundColor = MC_SKYBLUE_COLOR;
    [planBtn addTarget:self action:@selector(liRuanPlanBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}

/**
 *
 *MiddleTabView
 *
 **/
- (void)setUpMiddleTabView{
    
    [self setUpLeftMiddleView];
    [self setUpMiddleMiddeleView];
    [self setUPMiddleRightView];
}

- (void)setUpLeftMiddleView{
    UIView *middleView = [[UIView alloc] init];
    [self.baseScrollView addSubview:middleView];
    [middleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topFilterView.mas_bottom);
        make.left.equalTo(self.topFilterView.mas_left);
        make.width.equalTo(@(G_SCREENWIDTH));
        make.bottom.equalTo(self.view.mas_bottom).offset(-MC_REALVALUE(49));
    }];
    UIView *lineView = [[UIView alloc] init];
    
    lineView.backgroundColor = RGB(119, 119, 119);
    [middleView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(middleView);
        make.height.equalTo(@(1));
    }];
    NSArray *arr = @[@"序号",@"期数",@"倍数",@"金额"];
    for (NSInteger i = 0; i< 4; i++) {
        
        UILabel *label = [[UILabel alloc] init];
        [middleView addSubview:label];
        label.textColor = MC_SKYBLUE_COLOR;
        label.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
        label.text = arr[i];
        label.frame = CGRectMake(i * G_SCREENWIDTH * 0.25, MC_REALVALUE(5), G_SCREENWIDTH * 0.25, MC_REALVALUE(20));
        label.textAlignment = NSTextAlignmentCenter;
        
    }
    UITableView *middleTabView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    middleTabView.estimatedRowHeight = 2;
    [middleView addSubview:middleTabView];
    [middleTabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(MC_REALVALUE(31)));
        make.left.equalTo(self.view.mas_left);
        make.width.equalTo(@(G_SCREENWIDTH));
        make.bottom.equalTo(self.view.mas_bottom).offset(-MC_REALVALUE(49));
    }];
    middleTabView.delegate = self;
    middleTabView.dataSource = self;
    middleTabView.rowHeight = MC_REALVALUE(33);
    [middleTabView registerNib:[UINib nibWithNibName:NSStringFromClass([MCZhuihaoTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"zhuihaocell"];
    self.leftTableView = middleTabView;
}

- (void)setUpMiddleMiddeleView{

    UIView *middleView = [[UIView alloc] init];
    [self.baseScrollView addSubview:middleView];
    [middleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topFilterView.mas_bottom);
        make.left.equalTo(self.baseScrollView.mas_left).offset(G_SCREENWIDTH);
        make.width.equalTo(@(G_SCREENWIDTH));
        make.bottom.equalTo(self.view.mas_bottom).offset(-MC_REALVALUE(49));
    }];
    
    UIView *lineView = [[UIView alloc] init];
    
    lineView.backgroundColor = RGB(119, 119, 119);

    [middleView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(middleView);
        make.height.equalTo(@(1));
    }];
    
    NSArray *arr = @[@"序号",@"期数",@"倍数",@"金额"];
    for (NSInteger i = 0; i< 4; i++) {
        
        UILabel *label = [[UILabel alloc] init];
        [middleView addSubview:label];
        label.textColor = MC_SKYBLUE_COLOR;
        label.font = [UIFont systemFontOfSize:14];
        label.text = arr[i];
        label.frame = CGRectMake(i * G_SCREENWIDTH * 0.25, MC_REALVALUE(5), G_SCREENWIDTH * 0.25, MC_REALVALUE(20));
        label.textAlignment = NSTextAlignmentCenter;
        
    }
    UITableView *middleTabView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [middleView addSubview:middleTabView];
    [middleTabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(MC_REALVALUE(31)));
        make.left.equalTo(middleView);
        make.width.equalTo(@(G_SCREENWIDTH));
        make.bottom.equalTo(self.view.mas_bottom).offset(-MC_REALVALUE(49));
    }];
    middleTabView.delegate = self;
    middleTabView.dataSource = self;
    middleTabView.rowHeight = MC_REALVALUE(33);
    [middleTabView registerNib:[UINib nibWithNibName:NSStringFromClass([MCZhuihaoTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"zhuihaocell"];
    self.middleTableView = middleTabView;
 
}

- (void)setUPMiddleRightView{
    UIView *middleView = [[UIView alloc] init];
    [self.baseScrollView addSubview:middleView];
    [middleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topFilterView.mas_bottom);
        make.left.equalTo(self.baseScrollView.mas_left).offset(2*G_SCREENWIDTH);
        make.width.equalTo(@(G_SCREENWIDTH));
        make.bottom.equalTo(self.view.mas_bottom).offset(-MC_REALVALUE(49));
    }];
    UIView *lineView = [[UIView alloc] init];
    
    lineView.backgroundColor = RGB(119, 119, 119);
    [middleView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(middleView);
        make.height.equalTo(@(1));
    }];
    NSArray *arr = @[@"序号",@"期数",@"倍数",@"金额"];
    for (NSInteger i = 0; i< 4; i++) {
        
        UILabel *label = [[UILabel alloc] init];
        [middleView addSubview:label];
        label.textColor = MC_SKYBLUE_COLOR;
        label.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
        label.text = arr[i];
        label.frame = CGRectMake(i * G_SCREENWIDTH * 0.25, MC_REALVALUE(5), G_SCREENWIDTH * 0.25, MC_REALVALUE(20));
        label.textAlignment = NSTextAlignmentCenter;
        
    }
    UITableView *middleTabView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [middleView addSubview:middleTabView];
    [middleTabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(MC_REALVALUE(31)));
        make.left.equalTo(middleView);
        make.width.equalTo(@(G_SCREENWIDTH));
        make.bottom.equalTo(self.view.mas_bottom).offset(-MC_REALVALUE(49));
    }];
    middleTabView.delegate = self;
    middleTabView.dataSource = self;
    middleTabView.rowHeight = MC_REALVALUE(33);
    [middleTabView registerNib:[UINib nibWithNibName:NSStringFromClass([MCZhuihaoTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"zhuihaocell"];
    self.rightTableView = middleTabView;

}
/**
 *
 *MiddleTabView
 *
 **/
- (void)setUpBottomView{
    
    UIView *bottomInfoView = [[UIView alloc] init];
    [self.view addSubview:bottomInfoView];
    [bottomInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.equalTo(@(MC_REALVALUE(49)));
    }];
    bottomInfoView.backgroundColor = RGB(241, 241, 241);
    self.bottomInfoView = bottomInfoView;
    
    UIButton *stopBtn =  [[UIButton alloc] init];
    [bottomInfoView addSubview:stopBtn];
    stopBtn.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(12)];
    [stopBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bottomInfoView).offset(MC_REALVALUE(10));
        make.centerY.equalTo(bottomInfoView);
        make.top.equalTo(bottomInfoView).offset(MC_REALVALUE(13));
    }];
    
    [stopBtn setTitle:@"中奖停追" forState:UIControlStateNormal];
    [stopBtn setTitleColor:RGB(119, 119, 119) forState:UIControlStateNormal];
    [stopBtn setImage:[UIImage imageNamed:@"Choice_button_Unchecked"] forState:UIControlStateNormal];
    [stopBtn setImage:[UIImage imageNamed:@"Choice_button_selected"] forState:UIControlStateSelected];
    [stopBtn addTarget:self action:@selector(stopBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [stopBtn sizeToFit];
    
    MCPickBottomButton *payBtn = [[MCPickBottomButton alloc] init];
    [bottomInfoView addSubview:payBtn];
    payBtn.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(12)];
    payBtn.backgroundColor = RGB(0, 130, 210);
    [payBtn setTitle:@"付款" forState:UIControlStateNormal];
    [payBtn setImage:[UIImage imageNamed:@"tabbar_payment_selected"] forState:UIControlStateNormal];
    [payBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(bottomInfoView);
        make.width.equalTo(@(MC_REALVALUE(90)));
    }];
    
    UILabel *middleZHUSHULabel = [[UILabel alloc] init];
    middleZHUSHULabel.textColor = RGB(68, 68, 68);
    self.middleZHUSHULabel = middleZHUSHULabel;
    middleZHUSHULabel.font = [UIFont systemFontOfSize:MC_REALVALUE(12)];
    middleZHUSHULabel.textAlignment = NSTextAlignmentCenter;
    [bottomInfoView addSubview:middleZHUSHULabel];
    [middleZHUSHULabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(MC_REALVALUE(10)));
        make.centerX.equalTo(bottomInfoView);
        make.left.equalTo(stopBtn.mas_right);
        make.right.equalTo(payBtn.mas_left);
    }];
    
    UILabel *yuELabel = [[UILabel alloc] init];
    [bottomInfoView addSubview:yuELabel];
    self.yuELabel = yuELabel;
    yuELabel.textColor = RGB(68, 68, 68);
    yuELabel.font = [UIFont systemFontOfSize:MC_REALVALUE(12)];
    yuELabel.textAlignment = NSTextAlignmentCenter;
    [yuELabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bottomInfoView);
        make.bottom.equalTo(bottomInfoView).offset(-MC_REALVALUE(10));
        make.left.equalTo(stopBtn.mas_right);
        make.right.equalTo(payBtn.mas_left);
        
    }];

    self.middleZHUSHULabel.text = [NSString stringWithFormat:@"共%d注%d元",0,0];
    self.yuELabel.text = [NSString stringWithFormat:@"余额：%d元",0];
    [payBtn addTarget:self action:@selector(payBtnClick) forControlEvents:UIControlEventTouchDown];

}
- (void)setDefaultValue{
    self.startTF.text = @"1";
    self.zhuiTF.text = @"10";
    self.geFanBeiTF.text = @"1";
    self.zhuiFanBeiTF.text = @"10";
    self.beiFanBeiTF.text = @"2";
    self.startFanBeiTF.text = @"1";
    self.liRunStartTF.text = @"1";
    self.liRunZhuiTF.text = @"10";
    self.liRunTF.text = @"50";
    [self tongBeiPlanBtnClick];
}
#pragma mark - touch event


- (void)tongBeiPlanBtnClick{
    
        [self.view endEditing:YES];
        if ([self.startTF.text intValue]>=9999) {
            self.startTF.text = @"9999";
        }

        int count = [self.zhuiTF.text intValue];
        if (count>=120) {
        count = 120;
        self.zhuiTF.text = @"120";
        }
        if (count == 0) {
        [self.selectedCheckBoxArray removeAllObjects];
        }
        [self.selectedArray removeAllObjects];
        [self.selectedCheckBoxArray removeAllObjects];
    
        for (NSInteger i = 0; i< count; i++) {
            MCZhuihaoModel *model = [[MCZhuihaoModel alloc] init];
            model.title = [NSString stringWithFormat:@"%ld",i];
            model.selected = YES;
            [self.selectedCheckBoxArray addObject:model];
        }
        for (MCZhuihaoModel *model in self.selectedCheckBoxArray) {
            model.beishu = self.startTF.text;
        }
   
    [self.leftTableView reloadData];
}
- (void)fanBeiPlanBtnClick{
    
    [self.view endEditing:YES];
    if ([self.startFanBeiTF.text intValue]>=9999) {
        self.startFanBeiTF.text = @"9999";
    }

    int count = [self.zhuiFanBeiTF.text intValue];
    if (count>=120) {
        count = 120;
        self.zhuiFanBeiTF.text = @"120";
    }
    if (count == 0) {
        [self.selectedCheckBoxArray removeAllObjects];
    }
    [self.selectedArray removeAllObjects];
    [self.selectedCheckBoxArray removeAllObjects];
    
    if ([self.startFanBeiTF.text intValue] * [self.geFanBeiTF.text intValue]  *[self.beiFanBeiTF.text intValue] >9999 ) {
        [MBProgressHUD showError:@"倍数不能超过9999"];
        return;
    }
       int j = 1;
    for (NSInteger i = 0; i< count; i++) {
    

        if (i<[self.geFanBeiTF.text intValue]) {}
        else{
            if (i==[self.geFanBeiTF.text intValue]) {
                j = 1;
            } else {
                if ((i)%(([self.geFanBeiTF.text intValue ]))== 0) {
                    j*=[self.beiFanBeiTF.text intValue];
                }
                if (j* [self.startFanBeiTF.text intValue] * [self.beiFanBeiTF.text intValue] > 9999) {
                    break;
                }
            }
       
        }
        MCZhuihaoModel *model = [[MCZhuihaoModel alloc] init];
        model.title = [NSString stringWithFormat:@"%ld",i];
        model.selected = YES;
        [self.selectedCheckBoxArray addObject:model];
    }
    int k = 1;
    for (NSInteger i= 0;i<self.selectedCheckBoxArray.count;i++) {
        MCZhuihaoModel *model = self.selectedCheckBoxArray[i];
        
        
        if (i<[self.geFanBeiTF.text intValue]) {
            model.beishu = self.startFanBeiTF.text;
            continue;
        }
        else{
            if (i==[self.geFanBeiTF.text intValue]) {
                k = 1;
            } else {
                if ((i)%(([self.geFanBeiTF.text intValue ]))== 0) {
                    k*=[self.beiFanBeiTF.text intValue];
                }
                }
            
        }
         model.beishu = [NSString stringWithFormat:@"%d",k * ([self.startFanBeiTF.text intValue] * [self.beiFanBeiTF.text intValue])];

            }
    
    [self.middleTableView reloadData];
}


- (void)liRuanPlanBtnClick{
    int count = [self.liRunZhuiTF.text intValue];
    if (count>=120) {
        count = 120;
        self.zhuiFanBeiTF.text = @"120";
    }
    if (count == 0) {
        [self.selectedCheckBoxArray removeAllObjects];
    }
    [self.selectedArray removeAllObjects];
    [self.selectedCheckBoxArray removeAllObjects];
    
    if ([self.liRunStartTF.text intValue]>9999) {
        [MBProgressHUD showError:@"倍数不能超过9999"];
        return;
    }
    if ([self.liRunTF.text intValue]>99999) {
        [MBProgressHUD showError:@"利润率不能超过99999"];
        return;
    }
    // 单倍金额 80
    CGFloat nTotalMoney = 0;
    MCPaySelectedCellModel * model = self.dataSourceModel.dataSource[0];
    float signalMoney = [model.payMoney floatValue] / [model.stakeNumber floatValue];
    for (NSInteger i = 0; i<[self.liRunZhuiTF.text intValue]; i++) {
        CGFloat beiShuValue = ceilf((([self.liRunTF.text floatValue] /100.0 + 1) * nTotalMoney)/(1956 - (([self.liRunTF.text floatValue] /100.0 + 1) * signalMoney)));
       int max = MAX(beiShuValue, [self.liRunStartTF.text intValue]);
        nTotalMoney += max * 2;

        int shouYiValue = (1956 * [self.liRunStartTF.text intValue] - [self.liRunStartTF.text intValue] * 2)/[self.liRunStartTF.text intValue] * 2;
        if (shouYiValue/100<[self.liRunStartTF.text intValue]) {
            [MBProgressHUD showError:@"您设置的利润率过高，无法达到您的预期目标值，请重新修改参数设置"];
            return;
        }
        if (max>9999) {
            break;
        }
        MCZhuihaoModel *model = [[MCZhuihaoModel alloc] init];
        model.title = [NSString stringWithFormat:@"%d",i];
        model.beishu = [NSString stringWithFormat:@"%d",max];
        model.selected = YES;
        [self.selectedCheckBoxArray addObject:model];
        
    }
    [self.rightTableView reloadData];
    
}
- (void)stopBtnClick:(UIButton *)btn{
    btn.selected = !btn.selected;
}
- (void)payBtnClick{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确认付款" message:@"您一共消费10000万余元" preferredStyle:UIAlertControllerStyleAlert];
    // 添加按钮
    __weak typeof(alert) weakAlert = alert;
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"点击了确定按钮--%@-%@", [weakAlert.textFields.firstObject text], [weakAlert.textFields.lastObject text]);
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"点击了取消按钮");
    }]];
    [self presentViewController:alert animated:YES completion:nil];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];

}
#pragma mark - registerNotifacation
- (void)registerNotifacation{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)noti{
    NSDictionary *dic = noti.userInfo;
    CGRect rect = [dic[@"UIKeyboardBoundsUserInfoKey"] CGRectValue];
        [UIView animateWithDuration:0.25 animations:^{
    if (self.baseScrollView.contentOffset.x == 0) {
        self.leftTableView.contentOffset = CGPointMake(0, self.currentRect.origin.y);
        self.leftTableView.contentInset = UIEdgeInsetsMake(0, 0, rect.size.height, 0);
    } else if(self.baseScrollView.contentOffset.x == G_SCREENWIDTH){
        self.middleTableView.contentOffset = CGPointMake(0, self.currentRect.origin.y);
        self.middleTableView.contentInset = UIEdgeInsetsMake(0, 0, rect.size.height, 0);
    }else{
        self.rightTableView.contentOffset = CGPointMake(0, self.currentRect.origin.y);
        self.rightTableView.contentInset = UIEdgeInsetsMake(0, 0, rect.size.height, 0);
    }
    }];
    
    
}

- (void)keyBoardWillHide:(NSNotification *)noti{
    
    self.leftTableView.contentOffset = CGPointMake(0, 0);
    self.leftTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}

#pragma mark - UITableviewDelegate And DataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.selectedCheckBoxArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    MCZhuihaoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"zhuihaocell"];

    cell.textFiledDidBecomeFisrtR = ^(NSString *str,CGRect rect){
    
        self.currentRect = rect;
    
    };
    
    cell.textFiledDidSelected = ^(MCZhuihaoModel *model,NSString *lastTimeCount){
      
  
        if (model.selected == YES && ![self.selectedArray containsObject:model]) {
            [self.selectedArray addObject:model];
        } else if(model.selected == NO && [self.selectedArray containsObject:model]){
            [self.selectedArray removeObject:model];
        }
        int count = 0;
        for (MCZhuihaoModel *model in self.selectedArray) {
            count += [model.beishu intValue];
        }
        self.middleZHUSHULabel.text = [NSString stringWithFormat:@"共%d注%d元",count,count * 2];
        self.yuELabel.text = [NSString stringWithFormat:@"余额：%d元",count * 2];
    };
    
     cell.dataSource = self.selectedCheckBoxArray[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.view endEditing:YES];
//     MCZhuihaoModel *model = self.selectedCheckBoxArray[indexPath.row];
//     model.selected = !model.selected;
    
//    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
    
}

#pragma mark - textFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField{

  
}


- (void)segmentControlSelected:(UISegmentedControl *)segment{
    if (segment.selectedSegmentIndex == 0) {
        [self tongBeiPlanBtnClick];
    } else if(segment.selectedSegmentIndex == 1){
        [self fanBeiPlanBtnClick];
    }else{
        [self liRuanPlanBtnClick];
    }
    
    [self.baseScrollView setContentOffset:CGPointMake(segment.selectedSegmentIndex * G_SCREENWIDTH, 0) animated:YES];
    
}


#pragma mark - getter and setter 

- (NSMutableArray *)selectedCheckBoxArray{
    if (_selectedCheckBoxArray == nil) {
        _selectedCheckBoxArray = [NSMutableArray array];
    }
    
    return _selectedCheckBoxArray;
}


- (NSMutableArray *)selectedArray{
    if (_selectedArray == nil) {
        _selectedArray = [NSMutableArray array];
    }
    return _selectedArray;
}





@end
